package cn.sumpu.app.guoguojie.web.mvc.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.ProductBiz;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Order;
import cn.sumpu.app.guoguojie.domain.OrderProduct;
import cn.sumpu.app.guoguojie.domain.Product;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.PageModel;

@Controller
public class ProductOrderController {
	@Autowired
	private ProductBiz productBiz;
	@Autowired
	private IdentifierBiz identifierBiz;
	// 查看产品列表
	@RequestMapping(value = "/product.htm")
	public String product(Integer page ,HttpServletRequest request,HttpServletResponse response) throws IOException {
		try {       
			//今日推荐   
			Product recommendProduct =productBiz.selectRecommendProduct();
			
			List<Member> orderMemberList =productBiz.selectOrderMemberList();
			request.setAttribute("recommendProduct", recommendProduct);
			request.setAttribute("orderMemberList", orderMemberList);
			
			if (page == null) {
				page = 1;
			}
			int pageSize =20;
			int startId = (page - 1) * pageSize;
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", startId);
			map.put("pageSize", pageSize);
			map.put("category", "01");
			map.put("isClosed", "false");
			map.put("enabled", "true");
			//现金兑换列表   
			List<Product> productList =productBiz.selectProductList(map);
			Integer commentCount =productBiz.countProductList(map);
			request.setAttribute("productList", productList);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(productList); 
			pm.setPageSize(pageSize);    
			pm.setTotalRecord(commentCount); 
			pm.setTotalPage((commentCount + pageSize - 1) / pageSize);
			request.setAttribute("pageModelProduct", pm);
			request.setAttribute("page", page);
			
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "v2/product/product";
	}
	//产品列表
	@RequestMapping(value = "/productList.htm")
	public String productList(Integer page,HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		if (page == null) {
			page = 1;
		}
		int pageSize =20;
		int startId = (page - 1) * pageSize;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", startId);
		map.put("pageSize", pageSize);
		map.put("category", "01");
		map.put("isClosed", "false");
		map.put("enabled", "true");
		try {              
			//现金兑换列表   
			List<Product> productList =productBiz.selectProductList(map);
			Integer commentCount =productBiz.countProductList(map);
			request.setAttribute("productList", productList);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(productList); 
			pm.setPageSize(pageSize);    
			pm.setTotalRecord(commentCount); 
			pm.setTotalPage((commentCount + pageSize - 1) / pageSize);
			request.setAttribute("pageModelProduct", pm);
			request.setAttribute("page", page);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "v2/product/productList";
	}
	//订单页面
	@RequestMapping(value = "/orderProduct.htm")
	public String orderProduct(String productId,String buyNum,HttpServletRequest request,HttpServletResponse response) throws IOException {
			//订单页面 
			try {  
				Product product = productBiz.selectProduct(productId);
				request.setAttribute("buyNum", buyNum);
				request.setAttribute("product", product);
				request.setAttribute("productId", product.getIdentifier());
			} catch (AppBusinessException e) { 
				e.printStackTrace();
			}
		return "v2/product/orderProduct";
	}
	
	//下订单
	@RequestMapping(value = "/order.htm")
	public String order(Order order,OrderProduct orderProduct,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {            
			//下订单   
			Member member = (Member) request.getSession().getAttribute("member");
			int amount =Integer.valueOf(orderProduct.getQuantity());
			Product product =productBiz.selectProduct(orderProduct.getProduct());
			//orderProduct.setType("01");
			//String id =auxIdentifierBiz.createIdentifier(IdentifierType.ORDER);
			if(member!=null && product.getAmount()>=amount){   
				order.setIdentifier(identifierBiz.generateId("order"));
				order.setMember(member.getIdentifier());
				order.setFamily(member.getFamily());
				order.setNickname(member.getNickname());
				productBiz.order(order, orderProduct);
				pw = response.getWriter();
				pw.print("success");
			}else{
				return null;
			}
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	//我家的订单
	@RequestMapping(value = "/myOrderProduct.htm")
	public String myOrderProduct(Integer page,String startDate,String endDate,HttpServletRequest request,HttpServletResponse response) throws IOException {
			//订单页面 
 
			if (page == null) {
				page = 1;
			}
			try {  
				Member member = (Member) request.getSession().getAttribute("member");
				//家庭号查询订单
				if(member!=null){ 
					PageModel orderModel =productBiz.selectOrderListPageModel(page,member.getFamily(),startDate,endDate);
					request.setAttribute("orderModel", orderModel);
				}else{
					return "v2/index/index";
				}
			} catch (AppBusinessException e) { 
				e.printStackTrace();
			}
			return "v2/family/myOrderProduct";
		
	}
	//取消订单
	@RequestMapping(value = "/cancelOrder.htm")
	public String cancelOrder(String orderId,HttpServletRequest request,HttpServletResponse response) throws IOException {
			//订单页面 
			if(("").equals(orderId)){
				return null;
			}else{
			
				response.setContentType("text/html; charset=UTF-8");
				PrintWriter pw = null;
				try {  
					//取消订单 
					productBiz.cancelOrder(orderId);
					pw = response.getWriter();
					pw.print("success");
				} catch (AppBusinessException e) { 
					e.printStackTrace();
				}
			}
		return null;
	}
	//查看我家订单详情
	@RequestMapping(value = "/viewMyOrder.htm")
	public String myOrder(String orderId,HttpServletRequest request,HttpServletResponse response) throws IOException {
			try {   
				Order order =productBiz.selectViewOrder(orderId);
				request.setAttribute("order", order);
			} catch (AppBusinessException e) { 
				e.printStackTrace();
			}
		return "v2/product/viewMyOrder";
	}
}
